﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Base_ : MonoBehaviour
{
    protected SpriteRenderer sprite;
    public int hp;
    protected Grid currGrid;
    private void Start()
    {
        sprite = GetComponent<SpriteRenderer>();
    }
    /// <summary>
    /// 创建的初始化
    /// </summary>
    public void InitForCreat(bool inGrid)
    {
        sprite = GetComponent<SpriteRenderer>();
        if (inGrid)
        {
            sprite.sortingOrder = -1;
            sprite.color = new Color(1, 1, 1, 0.6f);
        }
        else
        {
            sprite.sortingOrder = 2;
        }
    }
    public virtual void InitForPlace(Grid grid)
    {
        currGrid = grid;
        transform.position = grid.wordPoint;
        currGrid.CurrPlantBase = this;
        sprite.sortingOrder = 0;
    }
    /// <summary>
    /// 各种颜色变化
    /// </summary>
    /// <returns></returns>
    protected IEnumerator ColorChange(float waittime, Color tragetcolor ,float time, UnityAction fun)
    {
        float currtime = 0; 
        float lerp;
        while (currtime < 7)
        {
            yield return new WaitForSeconds(0.05f);
            lerp = currtime / 7;
            currtime += 0.05f;
            sprite.color = Color.Lerp(Color.white, tragetcolor, lerp);
        }
        sprite.color = Color.white;
        if (fun != null) fun();
    }

    public void Hurt(int hurtValue)
    {
        hp -= hurtValue;
        StartCoroutine(ColorChange(0.2f,new Color(0.5f, 0.5f, 0.5f),0.05f, null));
          Debug.Log(hp);
        if (hp <= 0) Dead();
    }
    private void Dead()
    {
        Destroy(gameObject);
        currGrid.CurrPlantBase = null;
    }
    
   
    IEnumerator HurtColor(float waittime,Color color,float delaytime)
    {
        float currtime = 0; 
        float lerp;
        while(currtime<waittime)
        {
            yield return new WaitForSeconds(delaytime);
            lerp = currtime / waittime;
            currtime += delaytime;
            sprite.color = Color.Lerp(Color.white, color, lerp);
        }
        sprite.color = Color.white;
        
    }
}
